﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NLite.UI
{
    /// <summary>
    /// MVP 模式中的Presenter类
    /// </summary>
    [Serializable]
    public class Presenter : CompositeDisposable, IPresenter
    {

        /// <summary>
        /// 服务定位器
        /// </summary>
        public IServiceLocator Locator { get; private set; }

        /// <summary>
        /// 初始化Presenter
        /// </summary>
        public virtual void Init()
        {
            var it = Subscribe();
            while (it.MoveNext())
                AddDisposable(it.Current);
        }



        /// <summary>
        /// 订阅消息总线上的消息
        /// </summary>
        /// <returns></returns>
        public virtual IEnumerator<IDisposable> Subscribe()
        {
            yield break;
        }


        private IView view;
        /// <summary>
        /// View
        /// </summary>
        IView IPresenter.View
        {
            get { return view; }
        }

        /// <summary>
        /// 附加视图
        /// </summary>
        /// <param name="view"></param>
        protected virtual void AttachView(IView view)
        {
            this.view = view;
            AddDisposable(view);

           
            if (view.IsSupportGlobalization)
                view.InitializeResource();
        }

    
    }

    /// <summary>
    /// MVP 模式中的泛型Presenter类
    /// </summary>
    public class Presenter<TView> : Presenter, IPresenter<TView> where TView : IView
    {
        /// <summary>
        /// 泛型视图
        /// </summary>
        /// <remarks>懒惰初始化视图</remarks>
        /// 
        [Inject]
        public virtual new TView View
        {
            get
            {
                return (TView)(this as IPresenter).View;
            }
            protected set
            {
                AttachView(value);
            }
        }

    }
}
